<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/10/30
 * Time: 15:04
 */

namespace app\api\controller;


use language\Languagemsg;
use think\Db;

class Notice extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301, 'msg'=>$this->msg_language->otherLogin[$this->language]]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->msg_language->frost[$this->language]]);
            exit;
        }
    }
    
    /**
     * @我的
     * @return \think\response\Json
     * page    页码
     */
    public function atMeList(){
        $data = $this->atMe();
        if($data){
            foreach($data as $k => $v){
                // 动态@我
                $info = $this->informationForMe($v['notice_info_id']);
                $data[$k]['information'] = $info;
                if($v['notice_type'] == 7){ // 评论@我
                    // 评论
                    $comment = $this->commentForMe($v['notice_comment_id']);
                    $data[$k]['comment'] = $comment;
                }
                
                // 已读、未读
                if(strpos($v['notice_read_user'], ',' . $this->user_id . ',') !== false){
                    $data[$k]['is_read'] = 1; // 已读
                }else{
                    $data[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $data]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 评论
     * @return \think\response\Json
     * page    页码
     */
    public function commentMeList(){
        $data = $this->commentMe();
        if($data){
            foreach($data as $k => $v){
                // 动态
                $info = $this->informationForMe($v['notice_info_id']);
                $data[$k]['information'] = $info;
                if($v['notice_type'] == 8){
                    // 如果是评论  则需查出评论内容
                    $comment = $this->commentForMe($v['notice_comment_id']);
                    $data[$k]['comment'] = $comment;
                }else{
                    // 如果是回复，则需查出回复内容以及对应的评论或回复内容
                    $reply = $this->replyForMe($v['notice_reply_id']);
                    $data[$k]['reply'] = $reply;
                    if($reply['reply_type'] == 1){
                        $comment = $this->commentForMe($reply['reply_comment_id']);
                    }else{
                        $comment = $this->replyForMe($reply['reply_reply_id']);
                    }
                    $data[$k]['comment'] = $comment;
                }
                // 已读、未读
                if(strpos($v['notice_read_user'], ',' . $this->user_id . ',') !== false){
                    $data[$k]['is_read'] = 1; // 已读
                }else{
                    $data[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $data]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 点赞
     * @return \think\response\Json
     * page     页码
     */
    public function dzList(){
        $data = $this->dzMe();
        if($data){
            foreach($data as $k => $v){
                $info = $this->informationForMe($v['notice_info_id']);
                $data[$k]['information'] = $info;
                // 已读、未读
                if(strpos($v['notice_read_user'], ',' . $this->user_id . ',') !== false){
                    $data[$k]['is_read'] = 1; // 已读
                }else{
                    $data[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $data]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 系统消息
     * @return \think\response\Json
     * page   页码
     */
    public function systemList(){
        $data = $this->systemMe();
        if($data){
            foreach($data as $k => $v){
                // 已读、未读
                if(strpos($v['notice_read_user'], ',' . $this->user_id . ',') !== false){
                    $data[$k]['is_read'] = 1; // 已读
                }else{
                    $data[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $data]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 标记已读
     * @return \think\response\Json
     */
    public function readedNotice(){
        $notice_id = input('notice_id');
        $notice = Db::table('up_notice')
            ->where('notice_id', $notice_id)
            ->field('notice_read_user')
            ->find();
        if(strpos($notice['notice_read_user'], ',' . $this->user_id . ',') === false){
            $read_user = $notice['notice_read_user'] . $this->user_id . ',';
            Db::table('up_notice')->where('notice_id', $notice_id)->setField('notice_read_user', $read_user);
        }
        return json(['status' => 200, 'msg' => '']);
    }
    
    /**
     * 清空消息
     * @return \think\response\Json
     */
    public function delAllNotice(){
        $notice = $this->systemMe();
        if($notice){
            Db::startTrans();
            try{
                foreach($notice as $k => $v){
                    $del_user = $v['notice_del_user'] . $this->user_id . ',';
                    Db::table('up_notice')->where('notice_id', $v['notice_id'])->setField('notice_del_user', $del_user);
                }
                Db::commit();
//                return json(['status' => 200, 'msg' => '清空消息成功']);
                return json(['status' => 200, 'msg' => $this->msg_language->delAllNotice[$this->language]]);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => $e->getMessage()]);
            }
        }else{
//            return json(['status' => 200, 'msg' => '没有需要清空的消息']);
            return json(['status' => 200, 'msg' => $this->msg_language->noNotice[$this->language]]);
        }
    }
    
    /**
     * 未读消息数量
     * @return \think\response\Json
     */
    public function unreadNum(){
        // @我的消息数量
        $atMe_num = $this->atMe(2);
        // 评论我的消息数量
        $commentMe_num = $this->commentMe(2);
        // 点赞我的消息数量
        $dzMe_num = $this->dzMe(2);
        // 我的系统消息数量
        $mysystem_num = $this->systemMe(2);
        
        $total_num = $atMe_num + $commentMe_num + $dzMe_num + $mysystem_num;
        return json(['status' => 200, 'msg' => $total_num]);
    }
    
    /**
     * @我的消息
     * @param int $type    1 列表    2 统计未读数量
     * @return $this|false|int|\PDOStatement|string|\think\Collection
     */
    private function atMe($type = 1){
        $where['notice_type'] = ['IN', '6,7'];
        $where['notice_user'] = ['like', '%,' . $this->user_id . ',%'];
        $where['notice_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        if($type == 2){ // 统计未读消息数量
            $where['notice_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        }
        $data = Db::table('up_notice')
            ->where($where);
        if($type == 1){
            $data = $data->order('notice_time desc')
                ->page($this->page, 15)
                ->field('notice_id, notice_type, notice_info_id, notice_comment_id, notice_read_user, 
                notice_reply_id')
                ->order('notice_time desc')
                ->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 评论我的消息
     * @param int $type    1 列表    2 统计未读数量
     * @return $this|false|int|\PDOStatement|string|\think\Collection
     */
    private function commentMe($type = 1){
        $where['notice_type'] = ['IN', '8,9'];
        $where['notice_user'] = ['like', '%,' . $this->user_id . ',%'];
        $where['notice_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        if($type == 2){ // 统计未读消息数量
            $where['notice_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        }
        $data = Db::table('up_notice')
            ->where($where);
        if($type == 1){
            $data = $data->order('notice_time desc')
                ->page($this->page, 15)
                ->field('notice_id, notice_type, notice_info_id, notice_comment_id, notice_read_user, 
                notice_reply_id')
                ->order('notice_time desc')
                ->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 点赞我的消息
     * @param int $type    1 列表    2 统计未读数量
     * @return $this|false|int|\PDOStatement|string|\think\Collection
     */
    private function dzMe($type = 1){
        $where['notice_type'] = 10;
        $where['notice_user'] = ['like', '%,' . $this->user_id . ',%'];
        $where['notice_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        if($type == 2){ // 统计未读消息数量
            $where['notice_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        }
        $data = Db::table('up_notice')
            ->join('up_my_dz', 'dz_id = notice_dz_id', 'LEFT')
            ->join('up_u_user', 'user_id = dz_user_id', 'LEFT')
            ->where($where);
        if($type == 1){
            $data = $data->order('notice_time desc')
                ->page($this->page, 15)
                ->field('notice_id, notice_type, notice_info_id, notice_read_user, user_image, user_username, 
                dz_time')
                ->order('notice_time desc')
                ->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 我的系统消息
     * @param int $type    1 列表    2 统计未读数量
     * @return $this|false|int|\PDOStatement|string|\think\Collection
     */
    private function systemMe($type = 1){
        $where['notice_type'] = ['IN', '1,2,3,4,5'];
        $where['notice_user'] = [['like', '%,' . $this->user_id . ',%'], ['=', -1], 'or'];
        $where['notice_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        if($type == 2){ // 统计未读消息数量
            $where['notice_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        }
        $data = Db::table('up_notice')
            ->where($where);
        if($type == 1){
            $data = $data->order('notice_time desc')
                ->page($this->page, 15)
                ->field('notice_id, notice_type, notice_title, notice_content, notice_read_user, 
                notice_coupon_id, notice_yue_id, notice_order_id, notice_del_user, notice_time')
                ->order('notice_time desc')
                ->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 属于我的动态信息
     * @param $info_id   动态ID
     * @return array|false|\PDOStatement|string|\think\Model
     */
    private function informationForMe($info_id){
        $ceil = Db::table('up_information')
            ->join('up_u_user', 'user_id = info_user_id', 'LEFT')
            ->where('info_id', $info_id)
            ->field('info_user_id, user_username, user_image, info_content, info_images, info_video, 
            info_del, info_time, info_other_user')
            ->find();
        // 照片
        if($ceil['info_images']){
            $images = trim($ceil['info_images'], ',');
            $ceil['info_images'] = explode(',', $images);
            $ceil['info_thumb'] = $ceil['info_images'][0];
        }else{
            $ceil['info_images'] = [];
        }
        // 被@的用户
        $info_other_user = trim($ceil['info_other_user'], ',');
        $user_name = Db::table('up_u_user')->where('user_id', 'IN', $info_other_user)->column('user_username');
        $ceil['info_other_user'] = $user_name;
        return $ceil;
    }
    
    /**
     * 属于我的评论信息
     * @param $comment_id    评论ID
     * @return array|false|\PDOStatement|string|\think\Model
     */
    private function commentForMe($comment_id){
        $comment = Db::table('up_information_comment')
            ->join('up_u_user', 'user_id = comment_user_id', 'LEFT')
            ->where('comment_id', $comment_id)
            ->field('comment_user_id, user_username, user_image, comment_info_id, comment_content, 
            comment_other_user, comment_time, comment_status')
            ->find();
        $comment_other_user = trim($comment['comment_other_user']);
        $user_name = Db::table('up_u_user')->where('user_id', 'IN', $comment_other_user)->column('user_username');
        $comment['comment_other_user'] = $user_name;
        return $comment;
    }
    
    /**
     * 属于我的回复
     * @param $reply_id     回复ID
     * @return array|false|\PDOStatement|string|\think\Model
     */
    private function replyForMe($reply_id){
        $reply = Db::table('up_information_reply')
            ->join('up_u_user', 'user_id = reply_user_id', 'LEFT')
            ->where('reply_id', $reply_id)
            ->field('reply_user_id, user_username, user_image, reply_type, reply_info_id, reply_comment_id, 
            reply_reply_id, reply_content, reply_status, reply_time')
            ->find();
        return $reply;
    }
}